<?php
require_once 'role.php';
class office extends role{
	public function __construct($id){
		parent::__construct($id,'office');
	}
	public function request_x($data,$x){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		
		switch($x){
			case 1:$query = "delete from new_user where email_id='".$data['email']."'";
			$result = mysqli_query($connection,$query);
			//SEND THE USER A MAIL ABOUT HIS REQUEST
			break;
			case 2:
				
				if(strtolower($data['type'])=="phd"){
					$query = "call new_phd_insert('".$data['name']."','".$data['email']."')";
					$result = mysqli_query($connection,$query);
					if($result){
					echo "OK Done!";
				}
				else {
					echo "Something went wrong.";
					exit();
				}
				}
				else if(strtolower($data['type'])=="advisor"){
					$query = "call new_advisor_insert('".$data['name']."','".$data['email']."')";
					$result = mysqli_query($connection,$query);
					if($result){
					echo "OK Done!";
				}
				else {
					echo "Something went wrong.";
					exit();
				}
				}
				else if(strtolower($data['type'])=="office"){
					$query = "call new_office_insert('".$data['name']."','".$data['email']."')";
					$result = mysqli_query($connection,$query);
					if($result){
					echo "OK Done!";
				}
				else {
					echo "Something went wrong.";
					exit();
				}
				}
				else if(strtolower($data['type'])=="pgc"){
					$query = "call new_pgc_insert('".$data['name']."','".$data['email']."')";
					$result = mysqli_query($connection,$query);
					if($result){
					echo "OK Done!";
				}
				else {
					echo "Something went wrong.";
					exit();
				}
				}
				
				
		}
		//SEND THE USER A MAIL ABOUT HIS REQUEST
	}

	public function request_approval($req_id,$version_id){
		//$tdata = array("req_id"=>"","version_id"=>"");
		//$data = array($tdata);
		$db = new DatabaseManager();
		$connection = $db->getDB();
		
	$query="call change_status('".$this->getID()."',".$req_id.",".$version_id.",'approved')";
	$result=mysqli_query($connection,$query);
	$query="select phd_id from change_in_status join requests using(req_id,version_id) where req_id=".$req_id." && 
	version_id=".$version_id." group by req_id,version_id";
			/*$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();

	$result=mysqli_query($connection1,$query);
	$temp=mysqli_fetch_array($result,MYSQLI_ASSOC);
	$price = $temp['item_cost'];
	$pid = $temp['phd_id'];
	$query = "update phd set balance = SUM(balance - ".$price.") where phd_id = '".$pid."'"; */
	$result1=mysqli_query($connection,$query);
		$temp=mysqli_fetch_array($result1,MYSQLI_ASSOC);
		$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();
	$pid = $temp['phd_id'];
	$query="call change_balance(".$req_id.",".$version_id.",".$pid.")";
	
	$result=mysqli_query($connection,$query);
	
	
			$db2 = new DatabaseManager();
		$connection2 = $db2->getDB();

	$result=mysqli_query($connection2,$query);
	$db3 = new DatabaseManager();
		$connection3 = $db3->getDB();
		$query="select t1 from registered where id='".$pid."'";
		$result=mysqli_query($connection3,$query);
		$temp=mysqli_fetch_array($result,MYSQLI_ASSOC);
		$i=(int)$temp['t1'];
		if($i%10 == 0){
			$i+=1;
			$db4 = new DatabaseManager();
			$connection4 = $db4->getDB();
		$query="update registered set t1 = '".$i."' where id='".$pid."'";
		$result=mysqli_query($connection4,$query);
		}
	}
	public function get_new_user(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query = "select * from new_user";
		$result = mysqli_query($connection,$query);
		$tdata = array("name"=>"","email"=>"","type"=>"");
		$data = array($tdata);
		$i=0;
		if($result){
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				$tdata['name']=ucfirst($temp['name']);
				$tdata['email']=$temp['email_id'];
				$tdata['type']=ucfirst($temp['type']);
				$data[$i]=$tdata;
				$i++;
			}
			else
				return null;
		}
		else
			return null;
		return $data;
	}
	public function show_office_archives($connection){
	$query="call show_office_archives('".$this->getID()."')";
	$result=mysqli_query($connection,$query);
	
	$tdata = array("name"=>"","balance"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"","status"=>"","advisor"=>"");
		$data = array($tdata);
		$i=0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			$tdata['name']=$temp['name'];
			$tdata['balance']=$temp['balance'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=$temp['category'];
			$tdata['priority']=$temp['priority'];
			$tdata['time_stamp']=$temp['time_stamp'];
			$tdata['status']=$temp['status'];
			$tdata['advisor']=$temp['advisor'];
			$data[$i]=$tdata;
			$i++;
		}
		return $data;
	}
	
	
	public function get_requests_office($flag){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query="call get_request_status()";
		//$query = "call show_office(".$this->getID().")";
		$result = mysqli_query($connection,$query);
		$a=array("req_id"=>"","version_id"=>"");
		$b=array(array("req_id"=>"","version_id"=>""));		
		
		$i=0;
		$check = 0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			/*$tdata['name']=$temp['name'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=$temp['category'];
			$tdata['priority']=$temp['priority'];
			$tdata['time_stamp']=$temp['time_stamp'];
			$tdata['advisor']=$temp['advisor'];
			$data[$i]=$tdata;*/
			if($temp['status']==3 && $flag==0){
				
				$a['req_id']=$temp['req_id'];
				$a['version_id']=$temp['version_id'];

			$b[$i]=$a;
			$check++;
			$i++;
			}
			if($temp['status']==6 && $flag==1){
				
				$a['req_id']=$temp['req_id'];
				$a['version_id']=$temp['version_id'];

			$b[$i]=$a;
			$check++;
			$i++;
			}
			

		}
		mysqli_free_result($result);
		//mysqli_close($connection);
		if($check == 0){
			return null;
		}
		$i=0;
		
		
	$tdata = array("name"=>"","balance"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"",
	"advisor"=>"");
		$data = array($tdata);
		foreach($b as $t){
		$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();

		echo $query = "call get_request_office(".$t['req_id'].",".$t['version_id'].")";
			
			$result = mysqli_query($connection1,$query);
			
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				$tdata['name']=$temp['name'];
				$tdata['balance']=$temp['balance'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=ucfirst($temp['category']);
			$tdata['priority']=ucfirst($temp['priority']);
			$tdata['time_stamp']=$temp['time_stamp'];
			$tdata['advisor']=$temp['advisor'];
			$data[$i]=$tdata;
			$i++;
			}
			//exit();
		}
		return $data;
	}


}


?>